<template>
  <div class="my-container">
    <div class="header" v-if="!$store.getters['user/isLogin']">
      <img
        class="mobile-img"
        src="~@/assets/mobile.png"
        @click="$router.push({ name: 'login' })"
      />
      <div class="text">登录/注册</div>
    </div>
    <!-- 已登录 -->
    <div class="userInfo header" v-else>
      <!-- 基本信息 -->
      <div class="base">
        <div class="left">
          <van-image fit="cover" :src="user.photo" class="avatar" round />
          <span class="name">{{ user.name }}</span>
        </div>
        <div class="right">
          <van-button type="default" size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <!-- 粉丝、关注 -->
      <div class="data">
        <div class="data-item">
          <span>{{ user.art_count }}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{ user.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{ user.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{ user.like_count }}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <van-grid class="nav-grid" :column-num="2" clickable>
      <van-grid-item text="收藏">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span class="text" slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item text="文字">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span class="text" slot="text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <!-- 这个退出登录按钮只有登录后才显示 -->
    <van-cell
      v-if="$store.getters['user/isLogin']"
      class="logout-cell"
      clickable
      title="退出登录"
      center
      @click="logout"
    />
  </div>
</template>

<script>
export default {
  name: 'MyPage',
  data() {
    return {
      user: {},
    }
  },
  methods: {
    logout() {
      // 先给个确认提示
      this.$dialog
        .confirm({
          title: '你确定要退出吗？',
        })
        .then(() => {
          // 用户点了确定
          // 清空 vuex 中的 userToken（本地存储会自动清空）
          this.$store.commit('user/changeUserToken', undefined)
          // 跳转到登录页
          this.$router.push({ name: 'login' })
        })
    },
  },
  created() {
    // 如果 Vuex 中有 token，说明已经登录了，才去发获取用户信息的请求
    if (this.$store.getters['user/isLogin']) {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0, // 持续时间, 0 表示一直不关闭，默认 2000ms 后会自动关闭
      })
      this._apis.user
        .getUser()
        .then(res => {
          console.log('页面上居然成功了。。。res:', res)
          this.user = res.data.data
        })
        .finally(() => {
          this.$toast.clear() // 为了关闭转圈圈
        })
    }
  },
}
</script>

<style scoped lang="less">
.my-container {
  .logout-cell {
    height: 100px;
    text-align: center;
    color: #eb5253;
    margin: 10px 0;
    font-size: 30px;
  }
  > .header {
    width: 750px;
    height: 401px;
    background: url('~@/assets/banner.png') no-repeat;
    background-size: cover;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .mobile-img {
      width: 132px;
      height: 132px;
    }
    .text {
      color: #fff;
      font-size: 40px;
    }
  }
  .userInfo {
    // background: red;
    .base {
      width: 100%;
      height: 231px;
      padding: 76px 32px 23px;
      // background: #ccc;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 2px solid #fff;
        }
        .name {
          font-size: 29px;
          margin-left: 22px;
        }
      }
    }

    .data {
      width: 100%;
      display: flex;
      justify-content: space-around;
      .data-item {
        height: 130px;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        color: #fff;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 15px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
      }
    }
  }

  .nav-grid {
    background: #fff;
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .text {
      color: #000;
      margin-top: 8px;
      font-size: 28px;
    }
  }
}
</style>
